package com.asuka.mapper;

import com.asuka.dto.OrderSearchDTO;
import com.asuka.entity.OrderStatus;
import com.asuka.entity.Orders;
import com.asuka.entity.WorkspaceOrder;
import com.asuka.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {


    void insert(Orders order);

    void updateByNumber(Orders order);


    List<OrderVO> selectList(Orders order);

    void updateById(Orders order);

    List<Orders> search(OrderSearchDTO orderSearchDTO);

    @Select("select " +
            "id, number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark, phone, address, user_name, consignee, cancel_reason, rejection_reason, cancel_time, estimated_delivery_time, delivery_status, delivery_time, pack_amount, tableware_number, tableware_status " +
            "from " +
            "orders " +
            "where " +
            "id =#{id}")
    Orders selectById(Long id);


    @Select("select count(id) from orders where status = #{status}")
    Integer countStatus(Integer status);


    @Select("select " +
            "id, number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark, phone, address, user_name, consignee, cancel_reason, rejection_reason, cancel_time, estimated_delivery_time, delivery_status, delivery_time, pack_amount, tableware_number, tableware_status" +
            " from " +
            "orders " +
            "where " +
            "status = #{status} and order_time < #{time}")
    List<Orders> selectCancel(Integer status, LocalDateTime time);


    @Select("select " +
            "sum(amount) as amount,count(*) as count " +
            "from " +
            "orders " +
            "where " +
            "date (order_time) = #{now} and status = #{status}")
    WorkspaceOrder selectNowAmount(LocalDate now, Integer status);


    @Select("select count(*) from orders where date (order_time) = #{now}")
    Integer nowAllOrder(LocalDate now);

    @Select("select " +
            "    status,count(status) as count " +
            "from " +
            "    orders " +
            "group by " +
            "    status")
    List<OrderStatus> selectAllStatus();

    @Select("select count(*) from orders")
    Integer selectAllCount();
}
